package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {


    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    //查询今日运营数据
    /**
     * 营业额：当日已完成订单的总金额
     * 有效订单：当日已完成订单的数量
     * 订单完成率：有效订单数 / 总订单数
     * 平均客单价：营业额 / 有效订单数
     * 新增用户：当日新增用户的数量
     */
    @Override
    public BusinessDataVO businessData(LocalDateTime begin,LocalDateTime end) {

        HashMap map = new HashMap();
        map.put("begin", begin);
        map.put("end", end);

        //查询营业额
        Double totalTurnover = orderMapper.sumByMap(map);
        totalTurnover = totalTurnover == null ? 0 : totalTurnover;
        //查询总订单数
        Integer totalOrderCount = orderMapper.ordersByMap(map);

        //查询有效订单数
        map.put("status", Orders.COMPLETED);
        Integer validOrderCount = orderMapper.ordersByMap(map);

        //订单完成率
        Double orderCompletionRate = 0.0;
        //平均客单价
        Double unitPrice = 0.0;

        if(totalOrderCount != 0 && validOrderCount != 0) {
            //订单完成率
             orderCompletionRate = (double) validOrderCount / totalOrderCount;
            //平均客单价
             unitPrice = totalTurnover / validOrderCount;
        }

        //查询新增用户数
        Integer newUsers = userMapper.countByMap(map);
        return BusinessDataVO.builder()
                .turnover(totalTurnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();

    }

    //查询订单管理
    @Override
    public OrderOverViewVO overviewOrders() {
        Map map = new HashMap();
        map.put("begin", LocalDateTime.now().with(LocalTime.MIN));
        map.put("status", Orders.TO_BE_CONFIRMED);
        // 订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
        //待接单
        Integer waitingOrders = orderMapper.ordersByMap(map);

        //待派送
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.ordersByMap(map);

        //已完成
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.ordersByMap(map);

        //已取消
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.ordersByMap(map);

        //全部订单
        map.put("status", null);
        Integer allOrders = orderMapper.ordersByMap(map);

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    //查询菜品总览
    @Override
    public DishOverViewVO overviewDishes() {
        HashMap map = new HashMap();
        //查询已上架的菜品数量
        map.put("status", StatusConstant.ENABLE);
        Integer sold  = dishMapper.countByMap(map);
        //查询已下架的菜品数量
        map.put("status", StatusConstant.DISABLE);
        Integer unsold = dishMapper.countByMap(map);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(unsold)
                .build();
    }

    //查询套餐总览
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        HashMap map = new HashMap();
        //查询已上架的套餐数量
        map.put("status", StatusConstant.ENABLE);
        Integer sold  = setmealMapper.countByMap(map);
        //查询已下架的套餐数量
        map.put("status", StatusConstant.DISABLE);
        Integer unsold = setmealMapper.countByMap(map);

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(unsold)
                .build();
    }

}
